package edu.pku.ss.taas.cloud.monitor.lightweight;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.hyperic.sigar.CpuPerc;
import org.hyperic.sigar.FileSystemUsage;
import org.hyperic.sigar.Mem;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;

public class Monitor_backup extends Thread {
	private MonitorClientManager cache = MonitorClientManager.getInstance();
	private static Boolean isShuttingDown = false;

	private static Sigar sigar = SigarFactory.getSigar();
	
	public Monitor_backup(){}
	
	@Override
	public void run() {
		System.out.println("begin to monitor system:"+new Date());
		String hostIp = getHostIp();
		while (!isShuttingDown) {
			// report monitor data to memcache server
			Map<String, Double> result = collectMonitorDatas();
			
			
			cache.add(hostIp, result);

			isShuttingDown = Boolean.TRUE.equals(cache
					.get(Constants.SHUTTING_DOWN_COMMAND));
			System.out.println(new Date()+":"+result);
			try {
				Thread.currentThread().sleep(1000*5);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		System.out.println("is the monitor to quit:" + new Date());
	}

	public Map<String, Double> collectMonitorDatas() {
		Map<String, Double> result = new HashMap<String, Double>();

		try {
			result.put(Constants.CPU_USAGE, getCpuUsage());
			result.put(Constants.MEM_USAGE, getMemUsage());
			result.put(Constants.DISK_USAGE, getDiskUsage());

			System.out.println("collectMonitorDatas:" + result);
		} catch (SigarException e) {
			e.printStackTrace();
		}

		return result;
	}
	
	public String getHostIp(){
		return MonitorUtil.getLocalIP();
	}

	public double getCpuUsage() throws SigarException {
		CpuPerc cpuPerc = sigar.getCpuPerc();
		double cpuUsage = cpuPerc.getCombined();
		return cpuUsage;
	}

	public double getMemUsage() throws SigarException {
		Mem mem = sigar.getMem();
		double memUsage = mem.getUsedPercent();
		return memUsage;
	}

	public double getDiskUsage() throws SigarException {
		FileSystemUsage fsu = sigar.getFileSystemUsage(MonitorUtil.FILE_SYSTEM_TYPE);
		double diskUsage = fsu.getUsePercent();
		return diskUsage;
	}



	/**
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println("going to monitor system:"+new Date());
		Monitor_backup monitor = new Monitor_backup();
		System.out.println("ip:" + monitor.getHostIp());
		monitor.start();
		
//		while(!isShuttingDown){
//			BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//			try {
//				System.out.println("please input q to exit:");
//				String input = br.readLine();
//				
//				System.out.println("read input:"+input);
//				if("q".equals(input)){
//					System.out.println("bye!");
//					isShuttingDown = true;
//				}
//				
//			} catch (IOException e) {
//				e.printStackTrace();
//			}
//		}
		
	}
	
//	/**
//	 * @param args
//	 */
//	public static void main(String[] args) {
//		System.out.println("going to monitor system:"+new Date());
//		
//		System.out.println("ip:" + getHostIP());
//		Monitor m =  new Monitor();
//		m.run();
//		
//	}

}
